package com.lute.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.security.oauth2.common.json.JSONException;
import org.springframework.security.oauth2.common.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lute.utils.ServerErrorResponse;

@Controller
public class LogoutServlet {
	
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public @ResponseBody String logout(HttpServletRequest request) throws JSONException {
		String result = "";
		JSONObject jsonRes = new JSONObject();
		
		HttpSession session = request.getSession(false);
		if(!(session == null)) {
			try {
				session.invalidate();
				jsonRes.put("result", "OK");
			} catch(Exception e) {
				jsonRes.put("result", ServerErrorResponse.LOGOUT_FAILED.toString());
			}
			result = jsonRes.toString();
			return result;
		} else {
			jsonRes.put("result", ServerErrorResponse.USER_NOT_LOGGED_IN.toString());
			result = jsonRes.toString();
			return result;
		}
	}
}
